<?php
/**
 * Created by PhpStorm.
 * User: icemoon
 * Date: 2021-08-09
 * Time: 08:55
 */

namespace App\Http\Actions;


use App\Exceptions\EmailNotUniqueException;
use App\Models\User;

class CreateUser implements IAction
{
	protected $createBlog;

	public function __construct(CreateBlog $createBlog)
	{
		$this->createBlog = $createBlog;
	}

	public function execute(array $data)
	{
		$email = $data['email'];

		if(User::where('email',$email)->first())
		{
			throw new EmailNotUniqueException("$email should be unique.");
		}

		$user = new User;
		$user->name = $data['name'];
		$user->email = $data['email'];
		$user->password = $data['password'];
		$user->save();
		$blogData = [
			'user_id' => $user->id,
			'blog_name' => md5(uniqid(microtime(true),true))
		];

		$blog = $this->createBlog->execute($blogData);

		return $user;
	}
}